package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.entity.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.product.entity.BaseAttrInfo;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.mapper.BaseAttrInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class BaseAttrInfoServiceImpl extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo>
    implements BaseAttrInfoService{

    @Autowired
    BaseAttrValueService baseAttrValueService;

    @Override
    public List<BaseAttrInfo> getBaseAttrAndValue(Long category1Id, Long category2Id, Long category3Id) {
        return baseMapper.getBaseAttrAndValue(category1Id,category2Id,category3Id);
    }

    @Override
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {

        //1 保存BaseAttrInfo 信息到 base_attr_info
        this.save(baseAttrInfo);
        Long attrId = baseAttrInfo.getId();

        //2 保存BaseAttrValue 信息到 base_attr_value
        List<BaseAttrValue> baseAttrValueList = baseAttrInfo.getAttrValueList();
        //回存attrId属性
        for (BaseAttrValue baseAttrValue : baseAttrValueList) {
            baseAttrValue.setAttrId(attrId);
        }
        baseAttrValueService.saveBatch(baseAttrValueList);
    }

    @Override
    public void updateAttrInfo(BaseAttrInfo baseAttrInfo) {
        //修改BaseAttrInfo 信息到 base_attr_info
        this.updateById(baseAttrInfo);

        //修改BaseAttrValue 信息到 base_attr_value

        //attrValueList 里的 baseAttrValue 有删除，判断id是否在
        //前端传的id ：72 73
        //数据库里的id： 71 72 73  select id from base_attr_value where attr_id=15
        //71 不在前端传的里面 ,删除71
        //delete from base_attr_value where attr_id=15 not in (72,73);
        List<BaseAttrValue> baseAttrValueList = baseAttrInfo.getAttrValueList();
        List<Long> ids = new ArrayList<>();
        for (BaseAttrValue baseAttrValue : baseAttrValueList) {
            if (baseAttrValue.getId() != null){
                ids.add(baseAttrValue.getId());//前端传递的id集合
            }
        }
        //ids进行判断,是删除部分还是删除全部
        if (ids.size()>0){
            baseAttrValueService
                    .lambdaUpdate()//lambdaQuery 可以删除/修改
                    .eq(BaseAttrValue::getAttrId,baseAttrInfo.getId())
                    .notIn(BaseAttrValue::getId,ids)
                    .remove();
        }else {
            baseAttrValueService
                    .lambdaUpdate()
                    .eq(BaseAttrValue::getAttrId,baseAttrInfo.getId())
                    .remove();
        }
        //写法2
/*        LambdaQueryWrapper<BaseAttrValue> queryWrapper = new LambdaQueryWrapper<BaseAttrValue>()
                .eq(BaseAttrValue::getAttrId, baseAttrInfo.getId())
                .notIn(BaseAttrValue::getId, ids);
        baseAttrValueService.remove(queryWrapper);*/


        //attrValueList 里的 baseAttrValue  有修改有新增，根据id是否为空来进行判断
        for (BaseAttrValue baseAttrValue : baseAttrValueList) {
            if (baseAttrValue.getId()==null){
                //前端没有提交id的是 新增
                baseAttrValue.setAttrId(baseAttrInfo.getId());//回填 attrId
                baseAttrValueService.save(baseAttrValue);
            }else {
                //前端提交id的是 修改
                baseAttrValueService.updateById(baseAttrValue);
            }
        }
    }

}




